package com.lc.rongzecaifu.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lc.rongzecaifu.activity.InvestmentActivity;
import com.lc.rongzecaifu.conn.JsonInvest;
import com.lc.rongzecaifu.progress.SaundProgressBar;
import com.lc.rongzecaifu.rongzecaifu.R;

import java.text.NumberFormat;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2/22/2016.
 */
public class MyInvestlistAdapter_New extends RecyclerView.Adapter<MyInvestlistAdapter_New.ViewHolder> {

    private Context context;
    public final static int INVESTMODE = 1;//投资
    public final static int MANAGEFINANCESMODE = 2;//理财
    public final static int BONDMODE = 3;//债卷
    private int isInvest = 0;
    private List<JsonInvest.Info.ListContent> list;


    private SaundProgressBar mPbar;
    public static int progress = 0;
    private Message message;

    private String url;

    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            int p = msg.what;
            mPbar.setProgress(p);
        }

    };

    public MyInvestlistAdapter_New(Context context, List<JsonInvest.Info.ListContent> list, int isInvest) {
        this.context = context;
        this.list = list;
        this.isInvest = isInvest;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        @Bind(R.id.tv_rate)
        TextView tvRate;
        @Bind(R.id.tv_time)
        TextView tvTime;

        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_sumMoney)
        TextView tvSumMoney;
        @Bind(R.id.regularprogressbar)
        SaundProgressBar mPbar;

        public String str;
        public int a;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.list_invest_financial, null);
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        if (list.isEmpty()) {

        } else {


            holder.tvName.setText(list.get(position).borrow_name);
            NumberFormat nfNumber = NumberFormat.getNumberInstance();
            holder.tvRate.setText("年化率  " + list.get(position).borrow_interest_rate + "%");
            holder.tvTime.setText("期限  " + list.get(position).borrow_duration + list.get(position).borrow_duration_cn);


            holder.tvSumMoney.setText(list.get(position).need);
            holder.mPbar.setMax(100);
            Drawable indicator = context.getResources().getDrawable(
                    R.mipmap.jindu4);
            Rect bounds = new Rect(0, 0, indicator.getIntrinsicWidth(),
                    indicator.getIntrinsicHeight());
            indicator.setBounds(bounds);
            holder.mPbar.setProgressIndicator(indicator);
            holder.mPbar.setVisibility(View.VISIBLE);
            String string = list.get(position).progress;
            holder.mPbar.setmText(string);
            int a;
            holder.mPbar.setProgress((int) Float.parseFloat(list.get(position).progress));
        }

/*

        RxView.clicks(holder.itemView)
                .throttleFirst(2, TimeUnit.SECONDS)   //两秒钟之内只取一个点击事件，防抖操作
                .subscribe(new RecordingObserver<Object>() {
                    @Override
                    public void onNext(Object o) {
                        super.onNext(o);
//                        Toast.makeText(MainActivity.this, "点击了", Toast.LENGTH_SHORT).show();

                        Intent intent = new Intent();
                        intent.putExtra("id", list.get(position).id);
                        intent.setClass(context, InvestmentActivity.class);
                        context.startActivity(intent);

                    }

                });
*/

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (list.isEmpty() | list.size() == 0) {
                } else {
                    Intent intent = new Intent();
                    intent.putExtra("id", list.get(position).id);



          /*    new JsonAccessToken("index/borrow_info", new AsyCallBack<JsonAccessToken.Info>() {
                        @Override
                        public void onSuccess(String toast, int type, JsonAccessToken.Info info) throws Exception {
                            Log.e("Invett", "onSuccess");
                            if (info != null) {
                                if (!TextUtils.isEmpty(info.access_token)) {
                                    //   http://vhost119.zihaistar.com/api/index/invest_detail?id=3&show_type=borrow_info&access_token=4d2685fae1729bfa0d9ac59c939996a0
                                    url = "http://www.rongzecf.com/api/invest_detail?id=" + list.get(position).id + "&show_type=borrow_info" + "&access_token/" + info.access_token;
//                        webUrl1 = "https://www.haiying365.com/api/index/invest_detail?id=" + getIntent().getStringExtra("id") + "&show_type=borrow_info"+"&access_token="+info.access_token;

                                    Log.d("onsucess:webUrl1:", url);
                                    Log.d("onsucess:webUrl1:", url);
                                } else {
                                    //   UtilToast.show(InvestmentActivity.this, "安全验证失败！");
                                    LovelyToast.makeText(context, "安全验证失败", 0, LovelyToast
                                            .SUCCESS, LovelyToast.SCALE);
                                }
                            }
                        }

                        @Override
                        public void onFail(String toast, int type) throws Exception {
                            super.onFail(toast, type);
                            Log.e("Invett", "onSuccess");
                            // UtilToast.show(InvestmentActivity.this, "安全验证失败！");
                            LovelyToast.makeText(context, "安全验证失败", 0, LovelyToast
                                    .SUCCESS, LovelyToast.SCALE);
                        }

                        @Override
                        public void onStart(int type) throws Exception {
                            super.onStart(type);
                            Log.e("Invett", "onStart");
                        }

                        @Override
                        public void onEnd(String toast, int type) throws Exception {
                            super.onEnd(toast, type);
                            Log.e("Invett", "onEnd");
                        }
                    }).execute(context, true);

*/

                    intent.setClass(context, InvestmentActivity.class);
                    context.startActivity(intent);
                }


            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }





}
